import 'package:ele_progress/ele_progress.dart';
import 'package:flutter/material.dart';

class LoadingProgressUtils{
  static OverlayEntry? _overlayEntry;

  static void showLoading(BuildContext context,int progress){
    if(_overlayEntry != null) {
      _cancel();
    }
    _overlayEntry = OverlayEntry(builder: (BuildContext context) => _LoadingWidget(progress:progress));
    Overlay.of(context).insert(_overlayEntry!);
  }

  static void hideLoading(){
    _cancel();
  }

  static void _cancel(){
    _overlayEntry?.remove();
    _overlayEntry = null;
  }


}

class _LoadingWidget extends StatelessWidget {
  int? progress = 0;
  _LoadingWidget({Key? key,this.progress}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: (){
          LoadingProgressUtils.hideLoading();
        },
        child: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colors.black38,
            alignment: Alignment.center,
            child: Container(
              width: 130,
              height: 130,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20)),
                  color: Colors.white
              ),
              child: SizedBox(
                width: 70,
                height: 70,
                child: EProgress(
                  progress: progress??0,
                  type: ProgressType.circle,
                ),
              ),

            )
        )
    );
  }
}
